
#pragma once
#ifndef TRANSFER_H
#define TRANSFER_H

#include "myslam/common_include.h"
// #include "myslam/pic_sample.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <fstream>

using namespace std;
namespace myslam {

#pragma pack(1)

typedef struct Receive_data{
    uint16_t imu_sample_time;
    uint8_t pic_index;
    uint16_t pic_sample_time;
    int16_t GYRO[3];
    int16_t ACC[3];
    float q[4];
}ReceData;

typedef struct Trans_data{
    uint8_t head1;                          //0xf1
    uint8_t head2;                          //0xf2
    float t[3];                                     //pose[0-2]
    uint8_t valid_flag;                  //slam_valid_
    uint8_t tail2;                              //0xf2
    uint8_t tail1;                              //0xf1
    uint8_t sum;                              
}TransData;


union ReceData_Union{
    uint8_t data[33];
    ReceData recedata_struct;
};

union TransData_Union{
    uint8_t data[18];
    TransData transdata_struct;
};

#pragma pack()

enum RECE_STATUS {INIT=0,HEAD1,HEAD2,DATA,TAIL1,TAIL2};

class Transfer{
    public:
        EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
        typedef std::shared_ptr<Transfer> Ptr;
        Transfer();
        ~Transfer();

        int TransferInit(int fd, int nSpeed, int nBits, char nEvent, int nStop);
        void TransferStop();
        void Transfer_TransData(float t0,float t1,float t2,uint8_t valid);
        //TransferInit(fd,115200,8,'N',1);
        int fd;
    private:
        void TransferReceLoop();
        void TransferTranLoop();
        std::thread transferReceloop_thread_;
        std::thread transferTranloop_thread_;
        
        std::mutex data_mutex1_;
        std::atomic<bool> transfer_running1_;
        std::condition_variable transfer_rece_;
        std::mutex data_mutex2_;
        std::atomic<bool> transfer_running2_;
        std::condition_variable transfer_trans_;


        ofstream outfile;

       
        uint8_t receive_buffer[40];
        uint8_t transfer_buffer[40];
        ReceData_Union recedata;
        TransData_Union transdata;
        enum RECE_STATUS rece_status;

};

}  // namespace myslam


#endif